package com.example;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class MyClass {
    public static void main(String[] args) {
        Person person = new Person();
        System.out.println("Demo 包名 == "+person.getClass().getPackage().getName() +" 完整类名 == "+person.getClass().getName());

        Class<?> class1 = null;
        Class<?> class2 = null;

        try {
            class1 = Class.forName("com.example.Person");
            Person person1 = (Person) class1.newInstance();
            person1.setAge(18);
            person1.setName("zhangguobao");
            System.out.println("Demo 包名 == "+class1.getPackage().getName() +" 完整类名 == "+class1.getName());
            System.out.println("Person == "+person1.getName() +"  age == "+person1.getAge());

            Person person2 = null;
            Person person3 = null;
            Constructor<?>[] constructors = class1.getConstructors();
            person2 = (Person) constructors[1].newInstance();
            person2.setName("sda");
            person2.setAge(13);
            System.out.println(person2.toString());

            person3 = (Person) constructors[0].newInstance(22, "dsada");
            System.out.println(person3.toString());

            Class<?> class3 = null;
            class3 = Class.forName("com.example.Person");
            Object o = class3.newInstance();
            Field name = class3.getDeclaredField("name");
            name.setAccessible(true);
            name.set(o,"sha");
            System.out.println(name.get(o));

            System.out.println("-----------------------------");

            Class<?> class4 = null;
            class4 = Class.forName("com.example.SuperMan");
            Class<?> superClass = class4.getSuperclass();
            System.out.println("superClass == "+ superClass.getName() +"   class == "+ class4.getName());

            Field[] fields = class4.getDeclaredFields();
            Field[] superField = superClass.getFields();
            for (int i = 0; i < fields.length; i++) {
                System.out.println(" 类成员 "+fields[i]);
            }

            for (int i = 0; i < superField.length; i++) {
                System.out.println( "父类成员 "+superField[i]);
            }

            System.out.println("============================");

            Method[] methods = class4.getDeclaredMethods();
            for (int i = 0; i < methods.length; i++) {
                System.out.println(" method name == "+methods[i].getName());
                System.out.println(" method return type "+ methods[i].getReturnType());
                System.out.println(" method 访问修饰符 "+ Modifier.toString(methods[i].getModifiers()));
                System.out.println(" method return type "+ methods[i]);
            }

            System.out.println("----------===================");

            Class<?>[] interfaces = class4.getInterfaces();
            for (Class<?> anInterface : interfaces) {
                System.out.println("实现的接口名称 "+anInterface.getName());
            }

            Class<?> class5 = null;
            class5 = Class.forName("com.example.SuperMan");
            Method fly = class5.getMethod("fly");
            fly.invoke(class5.newInstance());
            Method walk = class5.getMethod("walk", int.class);
            walk.invoke(class5.newInstance(),100);
            System.out.println("类加载器的名字 == "+ class5.getClassLoader().getClass().getName());
            Method[] methods1 = class5.getMethods();
            for (Method method : methods1) {
                System.out.println(method.getName() +"  return type == "+method.getReturnType()+"  ");
                int size = method.getParameterTypes().length;
                for (int i = 0; i < size; i++) {
                    System.out.println("  paramter "+method.getParameterTypes()[i].getName());
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

//        class2 = Person.class;
//        System.out.println("Demo 包名 == "+class2.getPackage().getName() +" 完整类名 == "+class2.getName());


    }
}
